package com.sun.filesigndemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.sun.filesigndemo.bussiness.mapper.FileMapper;
import com.sun.filesigndemo.common.conf.Global;
import com.sun.filesigndemo.common.entity.File;
import com.sun.filesigndemo.common.security.UserUtils;
import com.sun.filesigndemo.common.statusCode.ResultMsg;
import com.sun.filesigndemo.common.utils.FileUtils;
import com.sun.filesigndemo.common.utils.UploadUtil;
import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("file")
public class FileController {

    private static String configPath = Global.get("app.upLoadPath").replaceAll("~",System.getProperty("user.home"));

    @Autowired
    FileMapper fileMapper;

    @RequestMapping(value = "upLoad", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getBasicInfo(HttpServletRequest request, MultipartFile file) throws IOException {
        //String pp = Global.get("fileOut.outputPath").replaceAll("~",System.getProperty("user.home"));

        String pp = configPath.replaceAll(("~"),System.getProperty("user.home"));

        UploadUtil upload = new UploadUtil();
        upload.setMaxSize(10000000);
        upload.setExts(".json|.txt");
        upload.setSavePath("mySourceFile/");


        if(upload.upload(request)){
            List list = upload.getFileNames();
            String str = (String) list.get(0);

            HashMap map = new HashMap();
            map.put("path",upload.getFileNamePath());

            //文件信息存储到数据库中
            File newFile = new File();
            //默认未标注
            newFile.init();
            newFile.setStatus(false);
            newFile.setFileName(file.getOriginalFilename());

            newFile.setPath(upload.getFileNamePath());
            newFile.setLastSignRow(1);
            newFile.setBelong(UserUtils.getUser().getName());




            if(fileMapper.selectByFileName(file.getOriginalFilename()) != null){
                return ResultMsg.successMsg("该文件已经添加过了");
            }else {



                fileMapper.insert(newFile);

                //上传成功以后新建该文件的样式文件
                String stylePath = pp+file.getOriginalFilename()+"style.txt";

                java.io.File styleFile = new java.io.File(configPath+ java.io.File.separator+"style" + java.io.File.separator+file.getOriginalFilename()+"style.txt");
                if(!styleFile.getParentFile().exists()){
                    java.io.File parent = styleFile.getParentFile();
                    parent.mkdirs();
                    styleFile.createNewFile();
                    //styleFile.createNewFile();
                }else if(!styleFile.exists()){
                    styleFile.createNewFile();
                }





                return ResultMsg.successData(map);
            }
//            File tem = fileMapper.selectByFileName(file.getOriginalFilename());
//            if(tem ==null){
//                fileMapper.insert(newFile);
//            }
        }else{
            return ResultMsg.fail(upload.getError());
        }
    }

    @RequestMapping(value = "allFile", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object>getAllFile(HttpRequest request){
        List list = fileMapper.selectAll();

        return ResultMsg.successData(list);
    }

    @RequestMapping(value = "getAllFilePartList", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object>getAllFileList(HttpRequest request){
        String username = UserUtils.getUser().getName();
        List list = fileMapper.selectByUserName(username);

        return ResultMsg.successData(list);
    }

    @RequestMapping(value = "partFile", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object>getFileNameAndIdList(HttpRequest request){
        List list = fileMapper.selectAllFileNameAndId();
        return ResultMsg.successData(list);
    }

    @RequestMapping(value = "changeBelong")
    @ResponseBody
    public Map<String, Object> changeBelong(HttpRequest request,String user,String file){
        String[] tem = file.split(",");
        JSONObject jsonObject = JSONObject.parseObject(file);

        int n = fileMapper.changeBelongByFileIdAndName((Integer) jsonObject.get("id"),jsonObject.get("fileName"),user);
        if(n>0){
            return ResultMsg.successMsg("修改成功");
        }else {
            return ResultMsg.fail("修改失败");
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteFile(HttpRequest request,File file){
        int m = 1;

        String pp = Global.get("fileOut.outputPath").replaceAll("~",System.getProperty("user.home"));

        String targetFilePath = pp+file.getFileName()+"_cp.txt";

        java.io.File targetFile = new java.io.File(pp+file.getFileName()+"_cp.txt");

        String stylePath = pp+file.getFileName()+"style.txt";

        java.io.File styleFile = new java.io.File(pp+file.getFileName()+"style.txt");

        //文件删除以后把样式文件顺便也删掉
        FileUtils.deleteFile(styleFile.getPath());



        FileUtils.deleteFile(file.getPath());
        //fileMapper.delete(file);
        //fileMapper.delete(file);
        int i = fileMapper.deleteByPrimaryKey(file.getId());
        if(i>0){
            return ResultMsg.successMsg("删除成功");
        }else {
            return ResultMsg.fail(("删除失败"));
        }
    }
}
